package com.cart.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.goods.model.GoodsService;
import com.goods.model.GoodsVO;
import com.likelist.model.Like_listVO;
import com.orderdetail.model.Order_detailVO;

public class CartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		doPost(req ,res);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		
		String action = req.getParameter("action");
		
		Order_detailVO likegood = (Order_detailVO) req.getAttribute("likelistgood");
		
		if(likegood instanceof Order_detailVO) {
			HttpSession session = req.getSession();
			List<Order_detailVO> cart = (List<Order_detailVO>) session.getAttribute("cart");
			
			boolean match = false;
			if (cart == null) {

				cart = new ArrayList<Order_detailVO>();

				cart.add(likegood);
			} else {
				for (Order_detailVO o : cart) {
					if (likegood.getGoodsno().equals(o.getGoodsno())) {
						o.setOrder_qt(o.getOrder_qt() + likegood.getOrder_qt());
						match = true;
					}
				}
				if (match == false) {
					cart.add(likegood);
				}
			}
			
			session.setAttribute("cart", cart);

			String url = req.getParameter("requestURL");
			RequestDispatcher dispatcher = req.getRequestDispatcher(url);
			dispatcher.forward(req, res);
		}
		
		
		if ("add_to_cart".equals(action)) {
			HttpSession session = req.getSession();
			List<Order_detailVO> cart = (List<Order_detailVO>) session.getAttribute("cart");
			
			Order_detailVO odVO = new Order_detailVO();
			odVO.setGoodsno(Integer.parseInt(req.getParameter("goodsno")));
			odVO.setOrder_qt(Integer.parseInt(req.getParameter("qty")));
			odVO.setPrice(Integer.parseInt(req.getParameter("price")));
			odVO.setGood_type(Integer.parseInt(req.getParameter("goodstype")));
						
			boolean match = false;
			if (cart == null) {

				cart = new ArrayList<Order_detailVO>();

				cart.add(odVO);
			} else {
				for (Order_detailVO o : cart) {
					if (odVO.getGoodsno().equals(o.getGoodsno())) {
						o.setOrder_qt(o.getOrder_qt() + odVO.getOrder_qt());
						match = true;
					}
				}
				if (match == false) {
					cart.add(odVO);
				}
			}
			
			session.setAttribute("cart", cart);

			String url = req.getParameter("requestURL");
			
			RequestDispatcher dispatcher = req.getRequestDispatcher(url);
			dispatcher.forward(req, res);
		}
		
		if("delete_from_cart".equals(action)) {
			HttpSession session = req.getSession();
			List<Order_detailVO> cart = (List<Order_detailVO>) session.getAttribute("cart");
			
			Integer goodsNo = Integer.parseInt(req.getParameter("goodsno"));
			
			for(int i = 0 ; i < cart.size(); i++) {
				if(goodsNo.equals(cart.get(i).getGoodsno())) {
					cart.remove(i);
				}
			}
			
			session.setAttribute("cart", cart);

			String url = req.getParameter("requestURL");

			RequestDispatcher dispatcher = req.getRequestDispatcher(url);
			dispatcher.forward(req, res);
		}
		
		if("transfer_to_list".equals(action)) {
			HttpSession session = req.getSession();
			List<Order_detailVO> cart = (List<Order_detailVO>) session.getAttribute("cart");
			
			Integer goodsNo = Integer.parseInt(req.getParameter("goodsno"));
			
			for(int i = 0 ; i < cart.size(); i++) {
				if(goodsNo.equals(cart.get(i).getGoodsno())) {
					cart.remove(i);
				}
			}
			
			session.setAttribute("cart", cart);
			
			Like_listVO lVO = new Like_listVO();
			lVO.setGoodsno(goodsNo);
			lVO.setMaccount(req.getParameter("maccount"));
			
			req.setAttribute("cartgood" ,lVO);
			
			RequestDispatcher dispatcher = req.getRequestDispatcher("/likelist/LikeServlet.do");
			dispatcher.forward(req, res);
		}
	}

}
